home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 114 / macaddict114.cdr / Software / Utilities / macam.0.8.4.dmg / macam sources / app_specific / MyScrollView.m < prev    next >
Encoding:
Text File  |  2002-05-22  |  5.4 KB  |  159 lines

  1. /*
  2.  macam - webcam app and QuickTime driver component
  3.  Copyright (C) 2002 Matthias Krauss (macam@matthias-krauss.de)
  4.  
  5.  This program is free software; you can redistribute it and/or modify
  6.  it under the terms of the GNU General Public License as published by
  7.  the Free Software Foundation; either version 2 of the License, or
  8.  (at your option) any later version.
  9.  
  10.  This program is distributed in the hope that it will be useful,
  11.  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  GNU General Public License for more details.
  14.  
  15.  You should have received a copy of the GNU General Public License
  16.  along with this program; if not, write to the Free Software
  17.  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18.  $Id: MyScrollView.m,v 1.2 2002/05/22 05:40:58 dirkx Exp $
  19.  */
  20.  
  21. #import "MyScrollView.h"
  22. #import "MyImageWindowController.h"
  23.  
  24. @implementation MyScrollView
  25.  
  26.  
  27. #define ZOOM_FIELD_WIDTH 60
  28.  
  29. - (void) awakeFromNib {
  30.     NSRect frame;
  31.     zoomFactor=1.0f;
  32.     frame.origin.x=0;
  33.     frame.origin.y=0;
  34.     frame.size.width=ZOOM_FIELD_WIDTH;
  35.     frame.size.height=15;
  36.     zoomField=[[NSPopUpButton alloc] initWithFrame:frame pullsDown:NO];
  37.     [zoomField setAlignment:NSRightTextAlignment];
  38.     [zoomField setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
  39.     [zoomField addItemsWithTitles:[NSArray arrayWithObjects:@"25%",@"50%",@"100%",@"200%",@"400%",NULL]];
  40.     [zoomField selectItemAtIndex:2];
  41.     [zoomField setTarget:self];
  42.     [zoomField setAction:@selector(zoomChanged:)];
  43.     [self addSubview:zoomField];
  44.     [zoomField setBordered:NO];
  45.     imageView=[[[NSImageView alloc] init] autorelease];    //Only retained by clipView
  46.     assert(imageView);
  47.     [imageView setImageAlignment:NSImageAlignCenter];
  48.     [self setDocumentView:imageView];
  49. }
  50.  
  51. - (void) dealloc {
  52.     [imageRep release];
  53.     [zoomField removeFromSuperview];
  54.     [zoomField release];
  55.     [super dealloc];
  56. }
  57.  
  58. - (void) zoomChanged:(id) sender {
  59.     switch ([zoomField indexOfSelectedItem]) {
  60.         case 0: [self setZoomFactor:0.25f]; break;
  61.         case 1: [self setZoomFactor:0.5f]; break;
  62.         case 2: [self setZoomFactor:1.0f]; break;
  63.         case 3: [self setZoomFactor:2.0f]; break;
  64.         case 4: [self setZoomFactor:4.0f]; break;
  65.     }
  66. }
  67.  
  68. - (void) tile {
  69.     NSRect myBounds,hScrollFrame,vScrollFrame,contentFrame,zoomFrame;
  70.     float hScrollerHeight,vScrollerWidth,zoomFieldHeight;
  71.     myBounds=[self bounds];
  72.     
  73. //Decide if we need the scroll bars
  74.     [self setHasHorizontalScroller:(imageSize.width>myBounds.size.width)];
  75.     [self setHasVerticalScroller:(imageSize.height>myBounds.size.height)];
  76.  
  77. //Get the width of scroll views
  78.     hScrollerHeight=[self hasHorizontalScroller]?15:0;
  79.     vScrollerWidth=[self hasVerticalScroller]?15:0;
  80.     zoomFieldHeight=15;
  81.     
  82. //Lay out the frames
  83.     zoomFrame.origin.x=myBounds.origin.x;
  84.     zoomFrame.origin.y=myBounds.origin.y+myBounds.size.height-zoomFieldHeight;
  85.     zoomFrame.size.width=ZOOM_FIELD_WIDTH;
  86.     zoomFrame.size.height=zoomFieldHeight;
  87.  
  88.     hScrollFrame.origin.x=zoomFrame.origin.x+ZOOM_FIELD_WIDTH;
  89.     hScrollFrame.origin.y=myBounds.origin.y+myBounds.size.height-hScrollerHeight;
  90.     hScrollFrame.size.width=myBounds.size.width-ZOOM_FIELD_WIDTH-vScrollerWidth;
  91.     hScrollFrame.size.height=hScrollerHeight;
  92.  
  93.     vScrollFrame.origin.x=myBounds.origin.x+myBounds.size.width-vScrollerWidth;
  94.     vScrollFrame.origin.y=myBounds.origin.y;
  95.     vScrollFrame.size.width=vScrollerWidth;
  96.     vScrollFrame.size.height=myBounds.size.height-hScrollerHeight;
  97.  
  98.     contentFrame.origin.x=myBounds.origin.x;
  99.     contentFrame.origin.y=myBounds.origin.y;
  100.     contentFrame.size.width=myBounds.size.width-vScrollerWidth;
  101.     contentFrame.size.height=myBounds.size.height-hScrollerHeight;
  102.     
  103.     [[self horizontalScroller] setFrame:hScrollFrame];
  104.     [[self verticalScroller] setFrame:vScrollFrame];
  105.     [[self contentView] setFrame:contentFrame];
  106.     [zoomField setFrame:zoomFrame];
  107.  
  108.     if (imageSize.width>contentFrame.size.width) contentFrame.size.width=imageSize.width;
  109.     if (imageSize.height>contentFrame.size.height) contentFrame.size.height=imageSize.height;
  110.  
  111.     [imageView setFrame:contentFrame];
  112. }
  113.  
  114. - (float) zoomFactor {
  115.     return zoomFactor;
  116. }
  117.  
  118. - (void) setZoomFactor:(float)zoom {
  119.     short idx;
  120.     if (zoomFactor==zoom) return;
  121.     if (zoom>3.0) { idx=4; zoomFactor=4.0f; }
  122.     else if (zoom>1.5) { idx=3; zoomFactor=2.0f; }
  123.     else if (zoom>0.75) { idx=2; zoomFactor=1.0f; }
  124.     else if (zoom>0.35) { idx=1; zoomFactor=0.5f; }
  125.     else { idx=0; zoomFactor=0.25f; }
  126.     [zoomField selectItemAtIndex:idx];
  127.     [self updateSize];
  128.     [[[self window] windowController] resizeWindowToContent];
  129. }
  130.  
  131. - (BOOL) setImageRep:(NSBitmapImageRep*)newRep {
  132.     if (imageRep) [imageRep autorelease];
  133.     imageRep=newRep;
  134.     if (newRep) [imageRep retain];
  135.     return [self updateSize];
  136. }
  137.  
  138. - (BOOL) updateSize {
  139.     NSImage* image=[[[NSImage alloc] init] autorelease];
  140.     if (!image) return NO;
  141.     [image setScalesWhenResized:YES];
  142.     [image addRepresentation:imageRep];
  143.     if (imageRep) imageSize=NSMakeSize((float)[imageRep pixelsWide]*zoomFactor,(float)[imageRep pixelsHigh]*zoomFactor);
  144.     else imageSize=NSMakeSize(1,1);
  145.     [image setSize:imageSize];
  146.     [imageView setImage:image];
  147. //   [self tile];
  148.     return YES;
  149. }
  150.  
  151. - (void)resizeSubviewsWithOldSize:(NSSize)oldSize {
  152.     [super resizeSubviewsWithOldSize:oldSize];
  153.     [self tile];
  154. }
  155.  
  156.  
  157.  
  158. @end
  159.